package com.FH.applyForm.officeServies.financegudget_firsttype;

import com.FH.applyForm.officeServies.financebudget_secondType.FinancesResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/13.
 */
@Stateless
public class FinanceDAOImpl extends AccessDB implements FinanceDAO{

    @Inject
    FinancesResource financesResource;
    /**
     * 查找财务预算以及费用
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getFinanceInfo(String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject();
            String existName = "0";

            String sqlexit = "select count(1) from officetool  where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok+1)+","+size+"";
            }

            if(json.has("officetool_id")){
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND officetool_id ='" + json.getString("officetool_id") + "'";
            }

            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();

            List<FinanceShowDTO> result = new ArrayList<FinanceShowDTO>();

            if(!existName.equals("0")){
                String sql ="SELECT officetool_id,officetool_type FROM officetool WHERE 1=1 "+sql_w +sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    FinanceShowDTO firstShowDTO = new FinanceShowDTO();
                    String officetool_id = seresult.get(i)[1];
                    firstShowDTO.setOfficetool_id(officetool_id);
                    firstShowDTO.setOfficetool_type(seresult.get(i)[2]);


                    ResultDTO userlist;
                    userlist=financesResource.getFinancesBudgetInfo("{officetool_id:"+officetool_id+"}","1","20");
                    firstShowDTO.setWs(userlist.getValues());

                    result.add(firstShowDTO);

                }
            }
            if(seresult.size()!=0) {

                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }


    /**
     * 添加财务预算一级菜单
     * @param financePullDTO
     * @return
     */
    @Override
    public ResultDTO addFinanceInfo(FinancePullDTO financePullDTO) {

        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String officetool_type=financePullDTO.getOfficetool_type();
        String sql="select * from officetool where officetool_type='"+officetool_type+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("信息已存在，请重新分配");
            return resultDTO;
        }
        String sql1="";
        sql1="insert into officetool (officetool_id,officetool_type)" +
                "values('"+uuid+"','"+financePullDTO.getOfficetool_type()+"')";

        resultls=addReportingTo(sql1);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    /**
     * 修改财务预算一级菜单
     * @param financePullDTO
     * @return
     */
    @Override
    public ResultDTO updateFinanceInfo(FinancePullDTO financePullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update advance_type set ";
        if (null != financePullDTO.getOfficetool_type() && !"".equals(financePullDTO.getOfficetool_type())) {
            QUERY += " advancetype_name = '" + financePullDTO.getOfficetool_type() + "'";
        }

        QUERY += " where advancetype_id = '" + financePullDTO.getOfficetype_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;


    }
    /**
     * 删除财务预算一级菜单
     * @param financePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteFinanceInfo(FinancePullDTO financePullDTO) {
        //删除之前首先查询该表中是否有记录

        String sql="select count(1) from officetool WHERE officetool_id='"+financePullDTO.getOfficetool_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from officetool  where officetool_id = '"+financePullDTO.getOfficetool_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {
            String sqlq = "delete from officetools where officetool_id = '"+financePullDTO.getOfficetool_id()+"'";
            int  results = this.addReportingTo(sqlq);
            if(results!=0){
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.delete_success);
            }
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
